package com.zbkj.crmeb.store.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.github.pagehelper.PageHelper;
import com.utils.DateUtil;
import com.utils.vo.dateLimitUtilVo;
import com.zbkj.crmeb.chant.service.IChannelMerchantService;
import com.zbkj.crmeb.store.dao.StoreOrderTransDao;
import com.zbkj.crmeb.store.dto.StoreOrderTransDTO;
import com.zbkj.crmeb.store.model.StoreOrderTrans;
import com.zbkj.crmeb.store.request.StoreOrderTransRequest;
import com.zbkj.crmeb.store.response.StoreOrderTransResponse;
import com.zbkj.crmeb.store.service.StoreOrderTransService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class StoreOrderTransServiceImpl extends ServiceImpl<StoreOrderTransDao, StoreOrderTrans> implements StoreOrderTransService {

    @Autowired
    StoreOrderTransDao storeOrderTransDao;

    /**
     * 功能描述: 传入dateLimit,查询规定时间内数据；传入订单id，根据订单id与商户id同时查询
     * @Param: [pageParamRequest, transRequest]
     * @Return: com.zbkj.crmeb.store.response.StoreOrderTransResponse
     * @Author: wangzh
     * @Date: 2022/9/23 11:27
     */
    @Override
    public StoreOrderTransResponse getList(PageParamRequest pageParamRequest, StoreOrderTransRequest transRequest) {
        String startTime = null;
        String endTime = null;

        //如果时间范围不为空，则按照时间条件查询
        if(StringUtils.isNotBlank(transRequest.getDateLimit())){
            dateLimitUtilVo dateLimitUtilVo = DateUtil.getDateLimit(transRequest.getDateLimit());
            startTime = dateLimitUtilVo.getStartTime();
            endTime = dateLimitUtilVo.getEndTime();
        }

        //查询所有数据，用于头部信息展示
        List<StoreOrderTrans> listAll = this.list(Wrappers.<StoreOrderTrans>lambdaQuery()
                .eq(null != transRequest.getMerId(), StoreOrderTrans::getMerId, transRequest.getMerId())
                .eq(null != transRequest.getOrderId(), StoreOrderTrans::getOrderId, transRequest.getOrderId())
                .between(StringUtils.isNotBlank(transRequest.getDateLimit()), StoreOrderTrans::getUpdateTime, startTime, endTime)
                .orderByDesc(StoreOrderTrans::getUpdateTime));

        //计算头部信息
        int count = listAll.size();
        BigDecimal costSum = listAll.stream().map(StoreOrderTrans::getTotalCostPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal taxSum = listAll.stream().map(StoreOrderTrans::getTotalTaxPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal totalPrice = costSum.add(taxSum);

        List<StoreOrderTransDTO> pageList = null;
        //分页查询数据
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        //如果传入订单id，根据订单id查询
        //如果传入商户id，根据商户id查询
        //两个都传入，同时查询
        //两个都不传，则查询所有数据
//        pageList = this.list(Wrappers.<StoreOrderTrans>lambdaQuery()
//                .eq(null != transRequest.getMerId(), StoreOrderTrans::getMerId, transRequest.getMerId())
//                .eq(null != transRequest.getOrderId(), StoreOrderTrans::getOrderId, transRequest.getOrderId())
//                .between(StringUtils.isNotBlank(transRequest.getDateLimit()), StoreOrderTrans::getUpdateTime, startTime, endTime)
//                .orderByDesc(StoreOrderTrans::getUpdateTime));
        pageList = storeOrderTransDao.getList(transRequest, startTime, endTime);

        return new StoreOrderTransResponse(count, totalPrice, costSum, taxSum, CommonPage.restPage(pageList));
    }
}
